package com.erp.erp_ui.warehouse.controller;

import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.warehouse.req.PlanInStoreStatistics1Req;
import com.erp.erp_entitys.warehouse.rsp.PlanInStoreStatistics1Rsp;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_ui.money.req.OperationReq;
import com.erp.erp_ui.warehouse.req.PlanInStorePageListReq;
import com.erp.erp_ui.warehouse.req.PlanInStoreReq;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.warehouse.req.ImportCarryDetailReq;
import com.erp.erp_ui.warehouse.service.PlanInstoreService;
import com.erp.erp_ui.warehouse.vo.*;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @ClassName : PlanInstoreController
 * @Description : 备货入库控制层
 * @Author : lst
 * @Date: 2020-07-01 18:25
 */
@Controller
@RequestMapping(value = "planInStore")
public class PlanInstoreController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(PlanInstoreController.class);

    /**
     * 备货入库业务逻辑接口
     */
    private final PlanInstoreService planInstoreService;

    @Autowired
    public PlanInstoreController(PlanInstoreService planInstoreService) {
        this.planInstoreService = planInstoreService;
    }

    /**
     * 首页-获取分页信息
     * @param req 查询条件实体
     * @return
     */
    @GetMapping("/getListByPage")
    @ResponseBody
    public R getListByPage(PlanInStorePageListReq req){
        try {
            LOGGER.info("备货入库-首页-获取分页信息:" + req.toString());
            PageInfo<PlanInStorePageListVo> pageInfo = planInstoreService.getListByPage(req);
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("备货入库-首页-获取分页信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 备货入库-新增-导出运明细分页查询
     * @param req 查询参数
     * @return 分页结果
     */
    @GetMapping("/add/getCarryDetailInfoByPage")
    @ResponseBody
    public R getCarryDetailInfoByPage(ImportCarryDetailReq req){
        try {
            LOGGER.info("备货入库-新增-导出运明细分页查询 req=" + req.toString());
            PageInfo<ImportCarryDetailVo> pageInfo = planInstoreService.getCarryDetailInfo(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("备货入库-新增-导出运明细分页查询异常" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 备货入库-新增-导出运明细分页查询-通过出运明细id获取出运明细产品信息
     * @param id 出运明细id
     * @return 分页结果
     */
    @GetMapping("/add/getCarryDetailChildInfoByPage")
    @ResponseBody
    public R getCarryDetailChildInfoByPage(Integer id){
        try {
            LOGGER.info("备货入库-新增-导出运明细分页查询-通过出运明细id获取出运明细产品信息 id=" + id);
            PageInfo<ImportCarryDetailProductVo> pageInfo = planInstoreService.getCarryDetailProductList(id);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("备货入库-新增-导出运明细分页查询-通过出运明细id获取出运明细产品信息异常" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 备货入库保存
     * @param req 新增数据实体
     * @return 新增结果
     */
    @PostMapping("/add")
    @ResponseBody
    public R add(@Validated @RequestBody PlanInStoreReq req){
        LOGGER.info("仓库管理-备货入库-新增 req=" + req.toString());
        try {
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return planInstoreService.add(req, name, code);
        }catch (Exception e){
            LOGGER.info("仓库管理-备货入库-新增异常：" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取详情
     * @param id 备货入库id
     * @return
     */
    @PostMapping("/detail")
    @ResponseBody
    public R getDetailInfo(@RequestBody Integer id){
        try {
            LOGGER.info("仓库管理-备货入库-获取详情 id=" + id);
            PlanInStoreDetailVo vo = planInstoreService.getDetailInfo(id, false);
            return R.build(vo);
        } catch (Exception e){
            LOGGER.info("仓库管理-备货入库-获取详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 修改-获取详情
     * @param id 备货入库id
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public R getUpdateInfo(@RequestBody Integer id){
        try {
            LOGGER.info("仓库管理-备货入库-修改-获取详情 id=" + id);
            PlanInStoreDetailVo vo = planInstoreService.getDetailInfo(id, true);
            return R.build(vo);
        } catch (Exception e){
            LOGGER.info("仓库管理-备货入库-修改-获取详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 删除
     * @param id id
     * @param status 状态(1.草稿箱 2.待批箱 3.已批箱 4.完成箱 5.垃圾箱)
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public R deleteInventoryInfo(@RequestBody Integer id, @RequestBody Integer status){
        try {
            LOGGER.info("仓库管理-备货入库-删除信息 id=" + id + ",status=" + status);
            planInstoreService.delete(id, status);
            return R.build("");
        }catch (Exception e){
            LOGGER.info("仓库管理-备货入库-删除信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 数据操作接口
     * @param req 操作类型
     * 1.提交审批 2.审批通过 3.审批退回 4.完成 5.还原
     * @return
     */
    @PostMapping("/operation")
    @ResponseBody
    public R operation(@RequestBody OperationReq req){
        try {
            LOGGER.info("仓库管理-备货入库-数据操作接口 req=" + req.toString());
            return planInstoreService.operation(req);
        }catch (Exception e){
            LOGGER.info("仓库管理-备货入库-数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }





    //***********************备货入库统计*****************************

    /**
     * 入库通知统计
     * @param req
     * @return
     */
    @RequestMapping("planInStoreStatistics1ListByPage")
    @ResponseBody
    public R planInStoreStatistics1ListByPage(PlanInStoreStatistics1Req req){
        try {
            LOGGER.info("仓库管理-备货入库-入库通知统计");
            PageInfo<PlanInStoreStatistics1Rsp> pageInfo = planInstoreService.planInStoreStatistics1ListByPage(req);
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("仓库管理-备货入库-入库通知统计功能异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }
    /**
     * 入库通知统计-导出
     * @param req
     * @return
     */
    @RequestMapping("planInStoreStatistics1Export")
    @ResponseBody
    public R planInStoreStatistics1Export(PlanInStoreStatistics1Req req){
        try {
            LOGGER.info("仓库管理-备货入库-入库通知统计");
            JSONObject info = planInstoreService.planInStoreStatistics1Export(req);
            return R.build(info);
        }catch (Exception e){
            LOGGER.info("仓库管理-备货入库-入库通知统计功能异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


}
